package com.quan.whizenoa.data.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.quan.commons.core.MyBaseServiceImpl;
import com.quan.commons.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.quan.whizenoa.data.commons.vo.OaHrUserVo;
import com.quan.whizenoa.data.entity.OaHrUser;
import com.quan.whizenoa.data.mapper.OaHrUserMapper;
import com.quan.whizenoa.data.service.OaHrUserService;


@Slf4j
@Service
@DS("whizenoa")
public class OaHrUserServiceImpl extends MyBaseServiceImpl<OaHrUserMapper, OaHrUser> implements OaHrUserService {

    @Autowired
    private OaHrUserMapper mapper;

    @Override
    public List<OaHrUserVo> queryList(PageUtils pageUtils, OaHrUserVo vo) {
        QueryWrapper<OaHrUser> queryWrapper = new QueryWrapper<OaHrUser>(vo);

        List<OaHrUser> list = super.list(pageUtils, queryWrapper);
        List<OaHrUserVo> collect = list.stream().map(item -> {
                OaHrUserVo bean = new OaHrUserVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, OaHrUserVo vo) {
        QueryWrapper<OaHrUser> queryWrapper = new QueryWrapper<OaHrUser>(vo);

        if (StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("name", vo.getKeyword());
            });
        }

        final IPage<OaHrUser> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<OaHrUser> records = page.getRecords();

        final List<OaHrUser> collect = records.stream().map((item) -> {
                OaHrUserVo bean = new OaHrUserVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }
}